/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class ControllerConstraint extends LineConstraint implements WiringConsumer {

    float minLength;
    float maxLength;
    float neutralLength;
    float range = .1f;

    public ControllerConstraint(Point a, Point b) {
        super(a, b);

        //set defaults for now
        minLength = (1.f - range) * length;
        maxLength = (1.f + range) * length;

        neutralLength = length;

    }

    public void validateLength() {
        super.validateLength();
        minLength = (1.f - range) * length;
        maxLength = (1.f + range) * length;

        neutralLength = length;
    }

    public void addLength(float a) {
        length += a;

        if (length > maxLength) {
            length = maxLength;
        }
        if (length < minLength) {
            length = minLength;
        }

        //System.out.println("adding length");

    }

    public void render() {

        if (selected == 1) {
            GL11.glColor3f(1.0f, 1.0f, 0.0f);
        } else {
            GL11.glColor3f(0.5f, 0.2f, 0.2f);
        }

        //points from a to b
        PVector towards = PVector.sub(b.pos, a.pos);
        towards.normalize();

        PVector minPoint = PVector.add(a.pos, PVector.mult(towards, minLength));

        GL11.glLineWidth(7.f);
        GL11.glBegin(GL11.GL_LINE_LOOP);                    // Drawing Using Triangles
        GL11.glVertex3f(a.pos.x, a.pos.y, a.pos.z);
        GL11.glVertex3f(minPoint.x, minPoint.y, minPoint.z);
        GL11.glEnd();

        GL11.glColor3f(1f, 1f, 1f);

        GL11.glLineWidth(2.f);
        GL11.glBegin(GL11.GL_LINE_LOOP);
        GL11.glVertex3f(minPoint.x, minPoint.y, minPoint.z);
        GL11.glVertex3f(b.pos.x, b.pos.y, b.pos.z);
        GL11.glEnd();

    }

    public ControllerConstraint(String[] line, Vector<Node> nodes) {
        this((Point) nodes.elementAt(Integer.parseInt(line[1])),
                (Point) nodes.elementAt(Integer.parseInt(line[2])));
    }

    public String pickle(Vector<Node> nodes) {
        return "C,"
                + nodes.indexOf(a) + ","
                + nodes.indexOf(b) + ",";


    }

    @Override
    public void setValue(float value) {
        length = neutralLength + value * range * length;
    }

    @Override
    public PVector getScreenCenter() {
        return getScreenSpaceCentroid();
    }
}
